﻿Imports AForge.Imaging.Filters
Imports AForge
Imports System.Drawing
Imports UCBase

Public Class LightAmpC
    Implements I_ImageExchange
    Private WithEvents Inp As I_ImageExchange
    Private Backpic As New BackgroundCreator
    Private Mov As New Mover

    Private WithEvents BuffIn As New UCBase.ImageBuffer
    Private WithEvents BuffPreOut As New UCBase.ImageBuffer
    Private Sub MoveToFilter()
        For c = 1 To KnobControl1.Value
            'Dim BM As New Bitmap(SourceBitmap)
            'Dim BMB As New Bitmap(SourceBitmap)
            'Dim filter As Add = New Add(BM)
            'DestBitmap = filter.Apply(BMB)
            'BM.Dispose()
            'BMB.Dispose()
            'SourceBitmap = DestBitmap
        Next c

    End Sub

    Public Event ImageOut(ByVal Image As System.Drawing.Bitmap) Implements I_ImageExchange.ImageOut1

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.BackgroundImage = Backpic.CreatePic(Me.Width, Me.Height, Color.MediumSlateBlue)
        Mov.Connect(Me)


        BuffIn.BufferSize = KnobControl1.Value
        BuffPreOut.BufferSize = KnobControl3.Value
    End Sub
    Private Sub Socket1_Connect() Handles Socket1.Connect
        Dim S As Socket = Socket1.Remote
        Inp = S.Parent
    End Sub

    Private Sub UniversalColorFilter_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        Me.Width = 600
        Me.Height = 255
    End Sub

    Private Sub Inp_ImageOut(ByVal Image As System.Drawing.Bitmap) Handles Inp.ImageOut1
        BuffIn.Add(Image)
    End Sub

    Private Sub KnobControl1_ValueChanged(ByVal Sender As Object)
        If KnobControl1.Value < KnobControl2.Value Then KnobControl1.Value = KnobControl2.Value
        BuffIn.BufferSize = KnobControl1.Value
        BuffIn.Clear()
    End Sub

    Private Sub Socket1_Disconnected() Handles Socket1.Disconnected
        Inp = Nothing
    End Sub
    Private Sub Socket1_EnableStateChanged(ByVal State As Boolean) Handles Socket1.EnableStateChanged
        Socket2.SocketsEnable = Socket1.SocketsEnable
        If Socket1.SocketsEnable = False Then
            BuffIn.Clear()
        End If
    End Sub
    Private Sub Buff_BufferImageAdded() Handles BuffIn.BufferImageAdded

        If BuffIn.BufferFull Then
            If KnobControl2.Value > 0 Then
                BuffIn.Lock()
                Dim BaseBmp As New Bitmap(BuffIn.GetBmp(True))
                For c = 1 To KnobControl2.Value
                    Dim filter As Add = New Add(BaseBmp)
                    BaseBmp = filter.Apply(BuffIn.GetBmp(True))
                Next c
                BuffPreOut.Add(BaseBmp)
                BaseBmp.Dispose()
                BuffIn.UnLock()
            Else
                BuffIn.Lock()
                BuffPreOut.Add(BuffIn.GetBmp(True))
                BuffIn.UnLock()
            End If
        End If

    End Sub

    Private Sub Buff_BufferLevel(ByVal Level As Integer) Handles BuffIn.BufferLevel
        BarGraph1.Value = Level
    End Sub

    Private Sub KnobControl3_ValueChanged(ByVal Sender As Object) Handles KnobControl3.ValueChanged
        BuffPreOut.BufferSize = KnobControl3.Value
        BuffPreOut.Clear()
    End Sub

    Private Sub BuffPreOut_BufferImageAdded() Handles BuffPreOut.BufferImageAdded
        RaiseEvent ImageOut(BuffPreOut.GetBmp)
    End Sub

    Private Sub BuffPreOut_BufferLevel(ByVal Level As Integer) Handles BuffPreOut.BufferLevel
        BarGraph2.Value = Level
    End Sub

    Private Sub KnobControl2_ValueChanged(ByVal Sender As Object) Handles KnobControl2.ValueChanged
        If KnobControl1.Value < KnobControl2.Value Then KnobControl1.Value = KnobControl2.Value
    End Sub
End Class
